function Store(date, database) {
	this._database = database;

	this.currentDate = date;

	this._activeSiteId = Store.dummySiteId;
	this._active = false;
}
Store.dummySiteId = "%%dummy";

Store.prototype = {
	get totalDuration() { 
		return this._database.getDailyTotal(this.currentDate); 
	},
	set totalDuration(duration) {
		this._database.updateOrCreateDailyTotal(this.currentDate, duration);
	},
	get siteDuration() { 
		return this._database.getSiteDuration(this._activeSiteId, this.currentDate); 
	},
	set siteDuration(duration) {
		this._database.updateOrCreateSiteSession(this._activeSiteId, this.currentDate, duration); 
	},
	get _activeSiteId() {
		return this.__activeSiteId;
	},
	set _activeSiteId(siteId) {		
		this.__activeSiteId = siteId;
	},
	get active() { 
		return (this._active && 
				this._activeSiteId != Store.dummySiteId);
	},
	set active(x) {
		debug("active = " + x);
		this._active = x;
	}
}

Store.prototype.increment = function S_increment(interval) {
	if (this.active) {
		var _interval = interval || 1;
		this.siteDuration += _interval;
		this.totalDuration += _interval;
	}
}

Store.prototype.decrement = function S_decrement(interval) {
	var _interval = interval || 1;
	this.siteDuration -= _interval;
	this.totalDuration -= _interval;
}

Store.prototype.changeSite = function S_changeSite(location) {
	var newSiteId, canBeActive;
	
	if (location === undefined) {
		location = { protocol: "" };
	}
	
	switch (location.protocol) {
	case "http:":
	case "https:":
		this._activeSiteId = location.hostname;
		this.active = true;
		break;
	case "file:":
		this._activeSiteId = location.pathname;
		this.active = true;
		break;
	default:
		//everything else e.g. about:,chrome:,javascript:
		this.active = false;
		this._activeSiteId = Store.dummySiteId;
	}
}

Store.prototype.triggerDateChange = function S_triggerDateChange(newDate) {
	this.flush();
	
	this.currentDate = newDate;
	this.siteDuration = 0;
	this.totalDuration = 0;
	
}

Store.prototype.flush = function S_flush() {
	this._database.save();	
}


